<!-- views/sales/components/CustomerOperationDialog.vue -->
<template>
  <el-dialog
    v-model="visible"
    :title="dialogTitle"
    width="500px"
    :before-close="handleClose"
  >
    <el-form 
      :model="form" 
      :rules="rules" 
      ref="formRef" 
      label-width="100px"
      @submit.prevent="handleSubmit"
    >
      <el-form-item label="客户名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入客户名称" />
      </el-form-item>
      
      <el-form-item label="客户编码" prop="code">
        <el-input v-model="form.code" placeholder="请输入客户编码" />
      </el-form-item>
      
      <el-form-item label="联系人信息" prop="contactPersons">
        <el-input v-model="form.contactPersons" type="textarea" placeholder="请输入联系人信息(JSON格式)" />
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%">
          <el-option label="启用" :value="1" />
          <el-option label="禁用" :value="0" />
        </el-select>
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">
        {{ editingCustomer ? '更新' : '保存' }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, defineProps, defineEmits, watch } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

interface Customer {
  id: number
  name: string
  code: string
  contactPersons: string
  status: number
  createdAt: string
  updatedAt: string
}

const props = defineProps<{
  modelValue: boolean
  editingCustomer: Customer | null
}>()

const emit = defineEmits<{
  (e: 'update:modelValue', value: boolean): void
  (e: 'submit', form: any): void
  (e: 'close'): void
}>()

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const formRef = ref<FormInstance>()
const submitting = ref(false)

const form = ref({
  name: '',
  code: '',
  contactPersons: '',
  status: 1
})

const dialogTitle = computed(() => {
  return props.editingCustomer ? '编辑客户' : '新增客户'
})

const rules = ref<FormRules>({
  name: [
    { required: true, message: '请输入客户名称', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入客户编码', trigger: 'blur' }
  ]
})

// 监听编辑项变化
watch(() => props.editingCustomer, (newVal) => {
  if (newVal) {
    form.value = { 
      name: newVal.name,
      code: newVal.code,
      contactPersons: newVal.contactPersons,
      status: newVal.status
    }
  } else {
    form.value = {
      name: '',
      code: '',
      contactPersons: '',
      status: 1
    }
  }
}, { immediate: true })

const handleClose = () => {
  visible.value = false
  emit('close')
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      submitting.value = true
      try {
        emit('submit', { ...form.value })
        handleClose()
        ElMessage.success(`${props.editingCustomer ? '更新' : '保存'}成功`)
      } catch (error) {
        ElMessage.error(`${props.editingCustomer ? '更新' : '保存'}失败`)
      } finally {
        submitting.value = false
      }
    }
  })
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 18px;
}

.el-divider {
  margin: 20px 0;
}
</style>